package com.student.controller;

import com.student.model.Teacher;
import com.student.model.TeacherDAO;
import com.student.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/teacher")
public class TeacherController {
    @Autowired
    private TeacherDAO teacherDAO;

    @Autowired
    private UserService userService;

    @PostMapping("/signUp")  //教师账号注册(添加教师)
    public ResponseEntity<?>  register(@RequestBody Teacher teacher){
        if(teacherDAO.existsByPhone(teacher.getPhone())){
            return new ResponseEntity<>("该教师已注册", HttpStatus.BAD_REQUEST);
        }else{
            return new ResponseEntity<Teacher>( teacherDAO.save(teacher),HttpStatus.OK);
        }
    }

    @PostMapping("/login")  //教师账号登录验证
    public ResponseEntity<?> login(String phone,String password){
       /* if(teacherDAO.existsByPhoneAndPassword(phone, password)){
            return new ResponseEntity<>(HttpStatus.OK);
        }else{
            return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
        }*/

       if(userService.checkTeacherPassword(phone, password)){
           return new ResponseEntity<>("登录成功",HttpStatus.OK);
       }else{
           return new ResponseEntity<>("登录失败",HttpStatus.BAD_REQUEST);
       }
    }

    @DeleteMapping("/delete")  //删除教师
    public ResponseEntity<?> delete(String phone){  //通过教师账号（手机号）删除
        if(!teacherDAO.existsByPhone(phone)){
            return new ResponseEntity<>("该教师不存在",HttpStatus.BAD_REQUEST);
        }else{
            teacherDAO.deleteByPhone(phone);
            return new ResponseEntity<>("删除成功",HttpStatus.OK);
        }
    }

    @GetMapping("/findOne")  //查一个教师:根据教工号(手机号)查一个教师
    public ResponseEntity<?> findOne(String phone){
        if(teacherDAO.existsByPhone(phone))
            return new ResponseEntity<>(teacherDAO.findTeacherByPhone(phone),HttpStatus.OK);
        return new ResponseEntity<>("该教师不存在",HttpStatus.BAD_REQUEST);
    }

    @GetMapping("/findAll")  //查所有教师
    public ResponseEntity<?> findAll(){
        return new ResponseEntity<>(teacherDAO.findAll(),HttpStatus.OK);
    }


}
